<?php
include_once(dirname(__FILE__) . "/StringUtils.php");

class CouponUtils
{
	private $arrSaveFilter = array();
	
	public function CouponUtils()
	{
		$this->arrSaveFilter[] = 'Save $';
		$this->arrSaveFilter[] = 'Take $';
		$this->arrSaveFilter[] = 'get a $';
		
	}
	
	function getMinSaveDollar($content)
	{
		preg_match_all('/\$([0-9,]+)*/', $content, $result);
		
		$arrMatchs = $result[1];
		
		$arr = array();
		foreach($arrMatchs as $v){
			$arr[] = str_replace(',', '', $v);
		}

		sort($arr);
		
		return $arr[0];
	}
	
	function getOffPercent($content)
	{
		$offValue = 0;
		
		preg_match_all('/([0-9]+)% off.*/i', $content, $result);
		
		$arrMatchs = $result[1];
		if(is_numeric($arrMatchs[0])){
			if($arrMatchs[0] < 101)
				$offValue = $arrMatchs[0];
		}
		
		preg_match_all('/save ([0-9]+)%.*/i', $content, $result);
		
		$arrMatchs = $result[1];
		if(is_numeric($arrMatchs[0])){
			if($arrMatchs[0] < 101)
				$offValue = $arrMatchs[0];
		}
		
		preg_match_all('/save up to ([0-9]+)%.*/i', $content, $result);
		
		$arrMatchs = $result[1];
		if(is_numeric($arrMatchs[0])){
			if($arrMatchs[0] < 101)
				$offValue = $arrMatchs[0];
		}
		
		preg_match_all('/up to ([0-9]+)%.*/i', $content, $result);
		
		$arrMatchs = $result[1];
		if(is_numeric($arrMatchs[0])){
			if($arrMatchs[0] < 101)
				$offValue = $arrMatchs[0];
		}

		preg_match_all('/get ([0-9]+)% off.*/i', $content, $result);
		
		$arrMatchs = $result[1];
		if(is_numeric($arrMatchs[0])){
			if($arrMatchs[0] < 101)
				$offValue = $arrMatchs[0];
		}
				
		return $offValue;
	}
	
	function getSaveDollar($content)
	{
		preg_match_all('/Save \$([0-9]+).*/i', $content, $result);
		
		$arrMatchs = $result[1];
		if(is_numeric($arrMatchs[0])){
			return $arrMatchs[0];
		}
		
		preg_match_all('/\$([0-9]+) off.*/i', $content, $result);
		
		$arrMatchs = $result[1];
		if(is_numeric($arrMatchs[0])){
			return $arrMatchs[0];
		}
		
		preg_match_all('/\$([0-9,]+) for.*/i', $content, $result);
		
		$arrMatchs = $result[1];
		
		$str = str_replace(',', '', $arrMatchs[0]);
		if(is_numeric($str)){
			return $str;
		}
		
		return 0;
	}
	
	function getOffNumber($content, $filterChar)
	{
		$ch		= '';
		$result = '0';
		
		$pos = stripos($content, $filterChar);
		
		if ($pos !== false) {
			$subContent = substr($content, 0, $pos);
			
			$spacePos = strripos($subContent, ' ');
			if($spacePos !== false){
				$num = substr($content, $spacePos, $pos - $spacePos);
				
				if(is_numeric($num)){
					$result = $num;
				}
			}
		}
		
		return $result;
	}

	function getSaveNumber($content, $filterStr)
	{
		$ch		= '';
		$result = '0';
		
		$pos = stripos($content, $filterStr);
		
		if ($pos !== false) {
			$startPos = $pos + strlen($filterStr);
			
			$endPos = stripos($content, ' ', $startPos);
			if($endPos !== false){
				$num = substr($content, $startPos, $endPos - $startPos);
				
				if(is_numeric($num)){
					$result = $num;
				}
			}
		}
		
		return $result;
	}
	
	function getSaveNumberByDefaultFilter($content)
	{
		$saveValue = 0;
		
		foreach($this->arrSaveFilter as $value){
			$saveValue = $this->getSaveNumber($content, $value);
			
			if($saveValue > 0)
				break;
		}
		
		return $saveValue;
	}
	
	public static function getCode($name, $startTag)
	{
		$pos 	= strripos($name, $startTag);
		
		if($pos){
			$str 	= substr($name, $pos + strlen($startTag));
			
			$str	= trim($str);
			$str	= trim($str, '-');
			$str	= trim($str, '.');
			$str	= trim($str);
			
			$arr 	= explode(' ', $str);
			$code 	= trim($arr[0], '.');
			
			$code 	= str_ireplace('Required', '', $code);
			$code 	= str_ireplace('needed', '', $code);
			$code 	= str_ireplace('for', '', $code);
			
			$code	= trim($code, '?');
			$code	= trim($code, '!');
			$code	= trim($code, ',');
			$code	= trim($code, '"');
			$code	= trim($code, ')');
			$code	= trim($code, '(');
			$code	= trim($code, '-');
			
			
			
			if(StringUtils::isCouponCode($code)){
				if(strlen($code) < 4){
					if(StringUtils::isUpperNum($code)){
						return $code;
					}else
						return '';
				}else
					return $code;
			}
		}
		
		return '';
	}
	
	public static function findCode($name)
	{
		$arrTags = array();
		$arrTags[]	= 'code ';
		$arrTags[]	= 'code:';
		$arrTags[]	= 'code?';
		$arrTags[]	= 'code.';
		$arrTags[]	= 'code=';
		$arrTags[]	= 'coupon :';
		$arrTags[]	= 'coupon:';
		$arrTags[]	= 'code :';
		
		foreach ($arrTags as $tag) {
			$code = CouponUtils::getCode($name, $tag);
			
			if(empty($code)){
				continue;
			}else{
				return $code;
			}
		}
		
		return '';
	}
}

?>
